package com.scau402.system1.domain.po;

import cn.hutool.core.annotation.Alias;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.common.utils.bean.BeanUtils;
import com.scau402.system1.domain.dto.WmsCheckBoundOrderDTO;
import com.scau402.system1.utils.IdGenerator;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 盘点单PO
 * @since 2024/1/20
 */
@Data
@TableName("wms_checkbound_order")
@NoArgsConstructor
public class WmsCheckBoundOrderPO {
    /**
     * 主键
     */
    @Alias("userId")
    private Long id;
    /**
     * 盘点单号
     */
    private String orderCode;
    /**
     * 盘点名称
     */
    private String orderName;
    /**
     * 创建者
     */
    private String createBy;
    /**
     * 备注
     */
    private String remark;
    /**
     * 盘点结果：0：不一致、1：一致
     */
    private Character checkResult;
    /**
     * 状态：0：正在盘点，1：盘点结束、2：结算中、3：盘点失败
     */
    private Character status;
    /**
     * 删除状态：0：正常、1：删除
     */
    private Character delFlag;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime updateTime;
    /**
     * 结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime endTime;

    /**
     * 创建人员id
     */
    private Long createById;

    /**
     * 租户id
     */
    private Long tenantId;

    /**
     * 创建盘点单PO
     * @param checkOrderDTO 盘点单DTO
     * @param idGenerator 自定义id生成器
     * @param user 用户信息
     * @param detailPO 需要创建的盘点单详情PO
     */
    public WmsCheckBoundOrderPO(WmsCheckBoundOrderDTO checkOrderDTO, IdGenerator idGenerator, SysUser user, WmsCheckboundOrderDetailPO detailPO){
        BeanUtils.copyProperties(checkOrderDTO,this);
        long checkOrderId = idGenerator.nextId("checkbound");
        this.id = checkOrderId;
        // TODO: 测试阶段盘点单号暂定为 PD+当前时间戳，后续根据实现修改
        String orderCode = "PD"+System.currentTimeMillis();
        this.orderCode = orderCode;
        this.createBy = user.getUserName();
        this.createTime = LocalDateTime.now();
        this.updateTime = this.createTime;
        this.status = '0';
        this.delFlag = '0';
        this.createById = user.getUserId();
        this.tenantId = user.getTenantId();
        //创建盘点单详情
        detailPO.build(idGenerator,checkOrderId,orderCode,user);
    }

    /**
     * 修改盘点状态以及设置盘点结果
     * @param isSame 是否与库存状态相同
     */
    public void stopCheck(boolean isSame) {
        this.checkResult = isSame ? '1' : '0';
        this.status = '1';
        this.endTime = LocalDateTime.now();
    }
}
